<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php';

//Người tạo : Nguyễn Thanh Hải - Team 2
//Module 	: Edit Lastest News Module
//Chức năng	: Quản lý việc Edit các thuộc tính cho  module Lastest News

class SupportOnline_ClickchangeController extends BaseUserController{

	public function init(){
		parent::init();
		$this->_helper->layout->disableLayout();
		$user = new User();
		$user->init();
		$user->check_login();
	}
	
	//Chức năng show các thuộc tính hiện tại cho người dùng
	public function indexAction(){
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/supportonline');
		
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
		$this->view->textTitle 		= $language->get('text_title');
		$this->view->textSort 		= $language->get('text_sort');
		$this->view->textSave 		= $language->get('text_save');
		$this->view->textError0 	= $language->get('text_error_0');
		$this->view->textError1 	= $language->get('text_error_1');
		$this->view->textError2 	= $language->get('text_error_2');
		$this->view->textError3 	= $language->get('text_error_3');
		$this->view->textError4 	= $language->get('text_error_4');
		$this->view->textError5 	= $language->get('text_error_5');
		$this->view->textError10 	= $language->get('text_error_10');
		/*========== End Text ==============*/
	
		//Start ------- Khởi tạo Parameter
		if(isset($_GET["url"]))
		{
			$this->view->url = $_GET["url"];
		}
		$path = APPLICATION_PATH."/widgets/supportonline/";
		$tbSidebarModule = new  SupportOnline_Model_SidebarModule();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Kiểm Tra file xml và truyền biến qua view
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		$user = new User();
			$user->init();
			$user->check_login();
			$hasmodify = $user->modifiy_widget($xmlconfig->name);
		if($hasmodify=="true")
	{
		if(isset($xmlconfig->name))
		{
			$module_supportonline = $tbSidebarModule->selectModuleByCode($xmlconfig->name);
			if(count($module_supportonline)>0)
			{
				$this->view->sort = $module_supportonline[0]["sort_order"];
				$this->view->name = $xmlconfig->name;
				$titleXML = 'title'.$name['name'];
				
					$this->view->title = $xmlconfig->$titleXML;
						if(isset($xmlconfig->$titleXML))
							{
										
								$this->view->title = "".$xmlconfig->$titleXML;
							}
							else if(isset($xmlconfig->titleDefault))
							{
								$this->view->title = "".$xmlconfig->titleDefault;
							}
							else 
							{
								$this->view->error = 1 ;
							}
						
			}
					else
					{
						$this->view->error = 1 ;
					}
			}
				else
				{
					$this->view->error = 1 ;
				}
	}
			else 
			{
				$this->view->error = 2 ;
			}
	}
	//Chức năng Edit lại các thuộc tính mà người dùng đã chọn
	public function editsupportAction(){
		$util= new Util();
		$tbsupport= new SupportOnline_Model_Support();
		
	if(isset($_GET["name"])&& strlen($_GET["name"])>=5 && strlen($_GET["name"])<=12)
	{
		$tbsupport= new SupportOnline_Model_Support();
		$id=$_GET["id"];
		$name=$_GET["name"];
		if(isset($_GET["yahoo"]))
			$yahoo=$_GET["yahoo"];
		else 
			$yahoo="chưa cập nhật";
		if(isset($_GET["skype"]))
			$skype=$_GET["skype"];
		else 
			$skype="chưa cập nhật";
		if(isset($_GET["posDefault"])&& strlen($_GET["posDefault"])>=5)
		{
			$tbLanguage = new Language_Model_Language();
				$languages = $tbLanguage->getAllLanguages();
				$lang['Default'] =  trim($_GET["posDefault"]);
				foreach ($languages as $language){
					if(isset($_GET["pos".$language['name']])){
						$lang[$language['name']] = trim($_GET["pos".$language['name']]);
					}else{
						$lang[$language['name']] = '';						
					}
				}
					
				$pos=serialize($lang);
			$now=time();
			if(isset($_GET["phone"]))
			{
				if($util->is_interger($_GET["phone"])&& strlen($_GET["phone"])<=11)
				{
					$phone=$_GET["phone"];
					$data_arr= array(
								'support_name'	=> $name,
								'support_position'=> $pos,
								'phone'=>$phone,
								'yahoo'=>$yahoo,
								'skype'=>$skype,
								'created_date'=>$now,	
								'updated_date'=>$now
										);
					$data=1;
					$result=$tbsupport->updateSupport($id,$data_arr);
					if($result==-1)
					{
						$data=-4;
					
					}
					
				}
				else {
					$data=-3;
				}
			}
			else 
			{
					$phone="";
					$data_arr= array(
								'support_name'	=> $name,
								'support_position'=> $pos,
								'phone'=>$phone,
								'yahoo'=>$yahoo,
								'skype'=>$skype,
								'created_date'=>$now,	
								'updated_date'=>$now
										);
					
					$data=1;
					$result=$tbsupport->updateSupport($id,$data_arr);
					if($result==-1)
					{
						$data=-4;
					
					}
			}
		}
		else {
			$data=-2;
		}
	}
	else 
	{
		$data=-1;
	}
	$this->_helper->json($data); 
	}
	public function addAction(){
	
		
	
			$util= new Util();
			$tbsupport= new SupportOnline_Model_Support();
	if(isset($_GET["name"])&& strlen($_GET["name"])>=5 && strlen($_GET["name"])<=12)
	{
		
			$tbsupport= new SupportOnline_Model_Support();
			
			$name=$_GET["name"];
			if(isset($_GET["yahoo"]))
				$yahoo=$_GET["yahoo"];
			else 
				$yahoo="chưa cập nhật";
			if(isset($_GET["skype"]))
				$skype=$_GET["skype"];
			else 
				$skype="chưa cập nhật";
		if(isset($_GET["posDefault"])&& strlen($_GET["posDefault"])>=5)
		{
				$tbLanguage = new Language_Model_Language();
				$languages = $tbLanguage->getAllLanguages();
				$lang['Default'] =  trim($_GET["posDefault"]);
				foreach ($languages as $language){
					if(isset($_GET["pos".$language['name']])){
						$lang[$language['name']] = trim($_GET["pos".$language['name']]);
					}else{
						$lang[$language['name']] = '';						
					}
				}
					
				$pos=serialize($lang);
				$now=time();
				if(($_GET["phone"])!="")
				{
					if($util->is_interger($_GET["phone"])&& strlen($_GET["phone"])<=11)
					{
						$phone=$_GET["phone"];
						$data_arr= array(
									'support_name'	=> $name,
									'support_position'=> $pos,
									'phone'=>$phone,
									'yahoo'=>$yahoo,
									'skype'=>$skype,
									'created_date'=>$now,	
									'updated_date'=>$now
											);
						$data=1;
						$result=$tbsupport->insertNewSupport($data_arr);
						if($result==-1)
						{
							$data=-4;
						
						}
						
					}
					else {
						$data=-3;
					}
				}
				else 
				{
						$phone="";
						$data_arr= array(
									'support_name'	=> $name,
									'support_position'=> $pos,
									'phone'=>$phone,
									'yahoo'=>$yahoo,
									'skype'=>$skype,
									'created_date'=>$now,	
									'updated_date'=>$now
											);
						
						$data=1;
						$result=$tbsupport->insertNewSupport($data_arr);
						if($result==-1)
						{
							$data=-4;
						
						}			
			}
		
		}
		else 
		{
			$data=-2;
		}
			
			
	}
	else 
	{
		$data=-1;
	}
	$this->_helper->json($data); 
	}

	public  function deleteAction() {
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/supportonline');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
		$textError10 	= $language->get('text_error_10');
		/*========== End Text ==============*/
		
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_widget($xmlconfig->name);
		if($hasmodify=="true")
		{
			$util= new Util();
			if(isset($_GET["id"])&& $util->is_interger($_GET["id"]) )
			{
				$id=$_GET["id"];
				$tbsupport= new SupportOnline_Model_Support();
				$resust=$tbsupport->deleteSupport($id);
				$data=1;
				if($resust==-1)
				{
					$data=-2;
				}
				
			}
			else 
			{
				$data=-100;
			}
			$this->_helper->json($data);
		}
		else 
		{
			$this->_helper->json($textError10);
		}
			 
		
	}
	public function editAction(){
			if(isset($_GET["name"]))
			{
				$tbSidebarModule = new  SupportOnline_Model_SidebarModule();
				$module_lastest  = $tbSidebarModule->selectModuleByCode($_GET["name"]);
				if(count($module_lastest)>0)
				{
					if(isset($_GET["title"])&&(strlen($_GET["title"])>=5))
					{
						if(isset($_GET["limit"])&&(is_numeric($_GET["limit"]))&&($_GET["limit"]>0))
						{
							if(isset($_GET["sort"])&&(is_numeric($_GET["sort"]))&&($_GET["sort"]>=0))
							{
								if($module_lastest[0]["sort_order"] == $_GET["sort"])
								{
									$modules = 0;
								}
								else 
								{
									$modules = $tbSidebarModule->selectModuleBySortOrder($_GET["sort"]);
								}
								if(($modules==0)||count($modules)<=0)
								{
									$tbSidebarModule->updateModule($_GET["name"],$_GET["sort"]);//Save Sort_Order in DB
									//Lấy ngôn ngữ hiện tại
									$language_id = $_SESSION['language'];
									$tbLanguage = new Language_Model_Language();
									$name = $tbLanguage->getNameLanguage($language_id);
									$title = title.$name['name'];
									
									//Start ------- Save title and limit in xml file
									$path = APPLICATION_PATH."/widgets/supportonline/";
									$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
									$xmlconfig->$title = $_GET['title'];
									//$xmlconfig->limit = $_GET['limit'];
									file_put_contents($path."config.xml", $xmlconfig->asXML());
									//End ------- Save title and limit in xml file
									
									$data = 1;	
									
								}
								else
								{
									$data = -5;
								}
							}
							else
							{
								$data = -4;
							}
						}
						else 
						{
							$data = -3;
						}
					}
					else 
					{
						$data = -2;
					}
				}
				else
				{
					$data = -1;
				}
			}
			else 
			{
				$data = -1;
			}
			$this->_helper->json($data); 
	}
}